﻿Imports Control.clases
Public Class Ubicaciones
    Inherits System.Web.UI.Page

    Dim oUbicacionLugar As New clsUbicacionLugar

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim dt As Data.DataTable
        If Not IsPostBack Then
            'Cargar Tabla
            dt = oUbicacionLugar.listarUbicacion
            GridView1.DataSource = dt
            GridView1.PageIndex = 0
            GridView1.DataBind()

            cmbDepende.DataSource = dt
            cmbDepende.DataTextField = "ubicacion_area"
            cmbDepende.DataValueField = "ubicacion_codigo"
            cmbDepende.DataBind()
        End If
    End Sub

    Protected Sub GridView1_PageIndexChanging(sender As Object, e As GridViewPageEventArgs) Handles GridView1.PageIndexChanging
        Dim dt As New Data.DataTable
        dt = oUbicacionLugar.listarUbicacion
        GridView1.DataSource = dt
        GridView1.PageIndex = e.NewPageIndex
        GridView1.DataBind()
        GridView1.SelectedIndex = 0
        Dim codigo As String = GridView1.Rows(0).Cells(1).Text
        cargarObjeto(codigo)
    End Sub

    Private Sub GridView1_RowCommand(sender As Object, e As GridViewCommandEventArgs) Handles GridView1.RowCommand
        If e.CommandName = "Select" Then
            Dim i As Integer = Integer.Parse(e.CommandArgument.ToString())
            Dim codigo As String = GridView1.Rows(i).Cells(1).Text
            cargarObjeto(codigo)
            btnModificar.Enabled = True
        End If
    End Sub

    Private Sub cargarObjeto(ByVal codigo As String)
        Try
            Dim dt As New Data.DataTable
            dt = oUbicacionLugar.buscarUbicacion(codigo)
            If dt.Rows.Count > 0 Then
                lblCodigo.Text = dt.Rows(0)("id_ubicacion").ToString()
                txtDescripcion.Text = dt.Rows(0)("ubicacion_area").ToString()
                txtCodigoUbicacion.Text = dt.Rows(0)("ubicacion_codigo").ToString()
                txtSiglas.Text = dt.Rows(0)("ubicacion_sigla").ToString()
                cmbDepende.SelectedValue = dt.Rows(0)("ubicacion_codigo").ToString()
            End If
        Catch ex As Exception
            Response.Redirect("Ubicaciones.aspx")
        End Try       
    End Sub

    Private Sub ConfigForm(ByVal opt_menu As String, ByVal bloqueo As Boolean)
        txtDescripcion.Enabled = Not bloqueo
        txtSiglas.Enabled = Not bloqueo
        cmbDepende.Enabled = Not bloqueo
        txtCodigoUbicacion.Enabled = Not bloqueo

        btnNuevo.Enabled = bloqueo '//BOTON NUEVO
        btnModificar.Enabled = bloqueo '//BOTON MODIFICAR
        btnGrabar.Enabled = Not bloqueo '//BOTON GRABAR
        btnCancelar.Enabled = Not bloqueo '//BOTON CANCELAR
        btnEliminar.Enabled = bloqueo '//BOTON ELIMINAR
        btnGrabar.CommandArgument = opt_menu '//ARGUMENTO A BOTON GRABAR
    End Sub

    Private Sub CleanForm()
        txtDescripcion.Text = ""
        cmbDepende.SelectedIndex = 0
        txtSiglas.Text = ""
        txtCodigoUbicacion.Text = ""
    End Sub

    Protected Sub btnNuevo_Click(sender As Object, e As EventArgs) Handles btnNuevo.Click
        ConfigForm("NEW", False)
        CleanForm()
    End Sub

    Protected Sub btnGrabar_Click(sender As Object, e As EventArgs) Handles btnGrabar.Click
        Try
            Dim oNuevo As New clsUbicacionLugar
            If btnGrabar.CommandArgument = "NEW" Then
                oNuevo.agregarUbicacion(txtDescripcion.Text,
                                        txtCodigoUbicacion.Text,
                                        cmbDepende.SelectedValue.ToString(),
                                        txtSiglas.Text)
                Response.Redirect("Ubicaciones.aspx")
            ElseIf btnGrabar.CommandArgument = "EDIT" Then
                oNuevo.actualizarUbicacion(lblCodigo.Text,
                                        txtDescripcion.Text,
                                        txtCodigoUbicacion.Text,
                                        cmbDepende.SelectedValue.ToString(),
                                        txtSiglas.Text)
                Response.Redirect("Ubicaciones.aspx")
            End If
            ConfigForm("SELECT", True)
            Dim dt As Data.DataTable
            dt = oNuevo.listarUbicacion
            GridView1.DataSource = dt
            If btnGrabar.CommandArgument = "NEW" Then
                GridView1.DataBind()
                GridView1.PageIndex = GridView1.PageCount - 1
                GridView1.Rows(GridView1.Rows.Count - 1).RowState = DataControlRowState.Selected
                Dim codigo As String = GridView1.Rows(dt.Rows.Count - 1).Cells(1).Text
                cargarObjeto(codigo)
            ElseIf btnGrabar.CommandArgument = "EDIT" Then
                Dim i_fila As Integer = GridView1.SelectedRow.RowIndex
                GridView1.Rows(i_fila).RowState = DataControlRowState.Selected
                Dim codigo As String = GridView1.Rows(i_fila).Cells(1).Text
                cargarObjeto(codigo)
            End If
            GridView1.DataBind()
        Catch ex As Exception
            Throw New System.Exception(ex.Message, ex.InnerException)
        End Try        
    End Sub

    Protected Sub btnModificar_Click(sender As Object, e As EventArgs) Handles btnModificar.Click
        ConfigForm("EDIT", False)
    End Sub

    Protected Sub btnCancelar_Click(sender As Object, e As EventArgs) Handles btnCancelar.Click
        Dim dt As Data.DataTable
        ConfigForm("SELECT", True)
        dt = oUbicacionLugar.listarUbicacion
        GridView1.DataSource = dt
        GridView1.DataBind()
        If dt.Rows.Count > 0 Then
        Else
            CleanForm()
        End If
    End Sub

    Protected Sub btnEliminar_Click(sender As Object, e As EventArgs) Handles btnEliminar.Click
        Try
            If GridView1.Rows.Count > 0 Then
                Dim page_ind As Integer = 0
                Dim i_fila As Integer = GridView1.SelectedRow.RowIndex
                Dim codigo As String = GridView1.Rows(i_fila).Cells(1).Text

                oUbicacionLugar.eliminarUbicacion(lblCodigo.Text)

                If GridView1.Rows.Count = 0 Or GridView1.PageCount = 0 Then
                    page_ind = 0
                Else
                    page_ind = GridView1.PageIndex
                End If

                Dim dt As Data.DataTable
                dt = oUbicacionLugar.listarUbicacion
                GridView1.DataSource = dt
                GridView1.PageIndex = page_ind
                GridView1.DataBind()
                CleanForm()
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
End Class